<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
$tblname = $_REQUEST["tblname"];

if ($tblname == "")
    $tblname = "tbl_pet";
$db = NEW myDBS();

switch ($tblname) {
    case "tbl_pet":
        $sql = "SELECT DISTINCT cheque_no, bank_name, pet_id, receiver, cheque_app_date, expire, SUM(net) AS net "
                . " FROM tbl_pet "
                . " WHERE payment_method = 'cheque' "
                . " AND cheque_status = 'อนุมัติ' "
                . " AND paid_status = 'รอจ่าย' "
                . " GROUP BY cheque_no, bank_name, pet_id, receiver, cheque_app_date, expire ";

        break;

    case "tbl_petition":
        $sql = "SELECT DISTINCT cheque_no, bank_name, p_id AS pet_id, company_name AS receiver, cheque_app_date, expire, SUM(inpNet) AS net "
                . " FROM tbl_petition "
                . " WHERE payment_method = 'cheque' "
                . " AND cheque_status = 'อนุมัติ' "
                . " AND paid_status = 'รอจ่าย' "
                . " GROUP BY cheque_no, bank_name, p_id, receiver, cheque_app_date, expire ";
        break;

    case "tbl_salary":
        $sql = "SELECT DISTINCT cheque_no, bank_name, pet_id, id AS receiver, cheque_app_date, expire, sum(salary) AS net "
                . " FROM  tbl_salary "
                . " WHERE  "
                . " cheque_no <> '' "
                . " AND cheque_status = 'อนุมัติ' "
                . " AND paid_status = 'รอจ่าย' "
                . " GROUP BY  cheque_no, bank_name, pet_id, id , cheque_app_date, expire ";
                //. " ORDER BY  cheque_no";

//        $sql = "SELECT cheque_no, bank_name, pet_id, card_id, cheque_app_date, expire, sum(salary) AS net "
//                . " FROM tbl_salary, tbl_payment, expenses_lvl1, expenses_lvl2 "
//                . " WHERE paid_status = 'รอจ่าย' "
//                . " AND tbl_salary.id = tbl_payment.card_id "
//                . " AND cheque_status = 'อนุมัติ' "
//                . " AND payment_method = 'cheque' "
//                . " GROUP BY cheque_no, bank_name, pet_id, card_id, cheque_app_date, expire ";

        break;

    default:
//        $sql = "SHOW TABLES";
//        $sql = "SELECT DISTINCT pet_id, expenses1_name, expenses2_name, net, cheque_no  "
//                . " FROM tbl_pet, expenses_lvl1, expenses_lvl2 "
//                . " WHERE payment_method = 'cheque' "
//                . " AND expenses_lvl1.expenses1_id = ex1_id "
//                . " AND expenses_lvl2.expenses2_id = ex2_id ";

        break;
}
//$sql = "SELECT DISTINCT pet_id, expenses1_name, expenses2_name, net "
//        . " FROM tbl_pet, expenses_lvl1, expenses_lvl2 "
//        . " WHERE payment_method = 'cheque' "
//        . " AND expenses_lvl1.expenses1_id = ex1_id "
//        . " AND expenses_lvl2.expenses2_id = ex2_id "
//        . " UNION ALL "
//        . "SELECT DISTINCT p_id AS pet_id , expenses1_name, expenses2_name, inpNet AS net"
//        . " FROM tbl_petition, expenses_lvl1, expenses_lvl2 "
//        . " WHERE payment_method = 'cheque' "
//        . " AND expenses_lvl1.expenses1_id = ex1_id "
//        . " AND expenses_lvl2.expenses2_id = ex2_id ";
//echo $sql;
$result = $db->runSQL($sql);
?>

<script type="text/javascript">
    $(document).ready(function() {
        $('#petition').dataTable();

        $("#tblname").change(function() {
            //alert($(this).val());
            window.location.href = "index.php?pages=rpt_cheque_nopaid&tblname=" + $(this).val();
        });
    });

//    function printCheque(id) {
//        var val = [];
//        var count = 0;
//        var tblname = "<?php echo $tblname; ?>";
//
//        $(':checkbox:checked').each(function(i) {
//            val[i] = $(this).val();
//            count++;
//            //alert(val[i]);
//        });
//
//        if (count == 0) {
//            alert("กรุณาเลือกข้อมูลที่ต้องการบันทึกด้วยครับ");
//        } else {
//            $('<div>').dialog2({
//                autoOpen: true,
//                title: "พิมพ์เช็ค",
//                content: "dlg_prt_cheque.php?val=" + val + "&tblname=" + tblname,
//                id: "modal-print"
//            });
//        }
//    }
</script>
<div class="row-fluid">
    <select id="tblname" name="tblname" >
        <option value="">เลือก...</option>
        <option value="tbl_pet" <?php echo ($tblname == "tbl_pet") ? "selected" : ""; ?> >อื่นๆ</option>
        <option value="tbl_petition" <?php echo ($tblname == "tbl_petition") ? "selected" : ""; ?>>โครงการ</option>
        <option value="tbl_salary" <?php echo ($tblname == "tbl_salary") ? "selected" : ""; ?>>เงินเดือน</option>
        <!--        <option value="pet">เงินเดือน</option>
                <option value="pet">สวัสดิการ</option>-->
    </select>
    <a href="javascript:print()" role="button" class="btn btn-primary" target="_blank"><i class="icon-print icon-white"></i>สั่งพิมพ์รายงาน</a>
</div>
<table id="petition" class="table table-bordered table-hover" width="100%" >
    <caption><br></caption>
    <thead>
        <tr>
            <th>เลขที่เช็ค</th>
            <th>ธนาคาร</th>
            <th>เลขที่ฎีกา</th>
            <th>ผู้รับเงิน</th>
            <th>วันที่อนุมัติ</th>
            <th>วันที่หมดอายุ</th>
            <th>จำนวนเงิน</th>

        </tr>
    </thead>
    <tbody>
        <?php
        while ($rows = $db->fetch_object($result)) {
            ?>
            <tr>
                <td>&nbsp;<?php echo $rows->cheque_no; ?></td>
                <td>&nbsp;<?php echo $rows->bank_name; ?></td>
                <td>&nbsp;<?php echo $rows->pet_id; ?></td>
                <td>&nbsp;
                    <?php 
                    if (is_numeric($rows->receiver)) {
                        echo get_name_person($rows->receiver);
                    } else {
                        echo $rows->receiver; 
                    }
                    
                    ?>
                </td>
                <td>&nbsp;<?php echo $rows->cheque_app_date; ?></td>
                <td>&nbsp;<?php echo $rows->expire; ?></td>
                <td><p class="text-right">&nbsp;<?php echo number_format($rows->net, 2); ?></p></td>

            </tr>
            <?php
        }
        ?>
    </tbody>
</table>

